<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <script src="js/jQuery.js"></script>
    <link rel="stylesheet" href="admin/css/pearCommon.css"/>
    <link rel="stylesheet" href="component/layui/css/layui.css"/>
    <script src="component/layui/layui.js"></script>
    <SCRIPT src="js/common.js"></SCRIPT>
    <style>
        .layui-form-label:after {
            content: "*";
            color: red;
            position: absolute;
            top: 5px;
            left: 15px;
        }
    </style>
</head>
<body>
<form class="layui-form" action="">
    <div class="mainBox">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form">
                <div class="layui-form-item">
                    <div hidden>
                        <input id="id"  name="id" class="layui-input" hidden>
                    </div>
                    <label class="layui-form-label">部门</label>
                    <div class="layui-input-block">
                        <select id="departmentId" name="departmentId" lay-filter="departmentId">
                            <!--  <input type="text" name="id" lay-verify="required" autocomplete="off"  class="layui-input">-->
                        </select>

                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">角色</label>
                    <div class="layui-input-block">
                        <input type="radio" name="role" value="0" title="普通用户" checked>
                        <input type="radio" name="role" value="1" title="管理员">
                    </div>
                </div>
                </div>
            </div>
        </div>
    </div>

    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="user-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>
<script>
    var departmentId={};
    layui.use(['pearHash', 'form', 'jquery'], function () {
        var form = layui.form, layer = layui.layer;
        var $ = layui.jquery;
        var pearHash = layui.pearHash;
        //自定义验证规则
        form.verify({
            required: function (value, elem) {
                if (!value) {
                    return '必填项不能为空！'
                }
            }
        });
        //layui表单提交方式，根据name属性提交
        //name值跟属性名一样
        var departmentresult = {};
        $(function () {
            //$("#selectRange").remove();//清空select列表数据
            var department = {}
            $.ajax({
                url: sessionStorage.getItem("rootPath") + "/departments/selectDepartmentList",
                method: "post",
                dataType: "json",
                contentType: 'application/json;charset=utf-8',
                data: JSON.stringify(department),
                success: function (res) {
                    departmentresult = res;
                    $("#departmentId").prepend("<option value='0'>请选择</option>");//添加第一个option值
                    console.log(departmentresult.data.length);
                    for (var i = 0; i < departmentresult.data.length; i++) {
                        $("#departmentId").append(new Option(departmentresult.data[i].departmentName, departmentresult.data[i].id));
                    }
                    layui.form.render("select");

                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
                }
            })
        })
        form.on('select(selectdepartmentid)',function (data){

        });
        form.on('submit(user-save)', function(data){
            //layui表单提交方式，根据name属性提交
            //name值跟属性名一样
            var user=data.field;
            console.log(JSON.stringify(data.field));
            $.ajax({
                url: sessionStorage.getItem("rootPath") + "/users/updateUser",
                data:JSON.stringify(user),
                dataType:'json',
                contentType: 'application/json;charset=utf-8',
                type:'post',
                async: false, // 使用同步操作
                timeout : 50000, //超时时间：50秒
                success:function(res){
                    if(res==1){
                        layer.msg("修改用户成功",{icon:1,time:1000},function(){
                            parent.getUserList(parent.curr-1,parent.pageSize);
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                        });
                    }else if(res==2){
                        layer.msg("修改用户失败！",{icon:0,time:1000});
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log("失败" + XMLHttpRequest.status + ":" + textStatus + ":" + errorThrown);
                }
            })
            return false;
        });
    });
</script>
<script>
</script>
</body>
</html>